package com.intech.android.demo.cms.servlets;

import com.intech.android.demo.cms.bo.Category;
import com.intech.android.demo.cms.bo.Group;
import com.intech.android.demo.cms.bo.Region;
import com.intech.android.demo.cms.dao.HibernateUtil;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;


public class InitServlet extends HttpServlet
{
    private static final Logger log = Logger.getLogger(InitServlet.class);
    private Session session;

    @Override
    public void init(final ServletConfig servletConfig) throws ServletException
    {
        super.init(servletConfig);
        log.info("Creation predestined objects");
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            log.info("Current session received =  [" + session.hashCode() + "]");
        }
        catch (Exception e)
        {
            log.error("!!!Cannot receive current session");
        }
        initData();
    }

    private void initData()
    {
        Transaction tx = session.beginTransaction();
        createGroups();
        createCategories();
        createRegions();
        tx.commit();
    }

    private void createRegions()
    {
        session.saveOrUpdate(new Region("Calcutta"));
        session.saveOrUpdate(new Region("Bangalore"));
        session.saveOrUpdate(new Region("Delhi"));
        session.saveOrUpdate(new Region("Mumbai"));
    }

    private void createCategories()
    {
        session.saveOrUpdate(new Category("Auto", "This category...", "i/src/preview1.png"));

        session.saveOrUpdate(new Category("Restaurants", "This category...", "i/src/preview2.png"));
        session.saveOrUpdate(new Category("Luxury", "This category...", "i/src/preview3.png"));
        session.saveOrUpdate(new Category("Sport and fitness", "This category...", "i/src/preview4.png"));
        session.saveOrUpdate(new Category("Loans and finance", "This category...", "i/src/preview5.png"));
        session.saveOrUpdate(new Category("Realty", "This category...", "i/src/preview.png"));
        session.saveOrUpdate(new Category("Notary and legal services", "This category...", "i/src/preview.png"));
        session.saveOrUpdate(new Category("Jewelry", "This category...", "i/src/preview.png"));
        session.saveOrUpdate(new Category("Accessories", "This category...", "i/src/preview.png"));
        session.saveOrUpdate(new Category("Hotels and resorts", "This category...", "i/src/preview.png"));
        session.saveOrUpdate(new Category("Health care", "This category...", "i/src/preview.png"));
        session.saveOrUpdate(new Category("Medical services", "This category...", "i/src/preview.png"));
        session.saveOrUpdate(new Category("High tech", "This category...", "i/src/preview.png"));
    }

    private void createGroups()
    {
        session.saveOrUpdate(new Group("M1518low"));
        session.saveOrUpdate(new Group("M1518med"));
        session.saveOrUpdate(new Group("M1518hi"));
        session.saveOrUpdate(new Group("M1518max"));
        session.saveOrUpdate(new Group("F1518low"));
        session.saveOrUpdate(new Group("F1518med"));
        session.saveOrUpdate(new Group("F1518hi"));
        session.saveOrUpdate(new Group("F1518max"));
        session.saveOrUpdate(new Group("M1921low"));
        session.saveOrUpdate(new Group("M1921med"));
        session.saveOrUpdate(new Group("M1921hi"));
        session.saveOrUpdate(new Group("M1921max"));
        session.saveOrUpdate(new Group("F1921low"));
        session.saveOrUpdate(new Group("F1921med"));
        session.saveOrUpdate(new Group("F1921hi"));
        session.saveOrUpdate(new Group("F1921max"));
        session.saveOrUpdate(new Group("M2227low"));
        session.saveOrUpdate(new Group("M2227med"));
        session.saveOrUpdate(new Group("M2227hi"));
        session.saveOrUpdate(new Group("M2227max"));
        session.saveOrUpdate(new Group("F2227low"));
        session.saveOrUpdate(new Group("F2227med"));
        session.saveOrUpdate(new Group("F2227hi"));
        session.saveOrUpdate(new Group("F2227max"));
        session.saveOrUpdate(new Group("M2835low"));
        session.saveOrUpdate(new Group("M2835med"));
        session.saveOrUpdate(new Group("M2835hi"));
        session.saveOrUpdate(new Group("M2835max"));
        session.saveOrUpdate(new Group("F2835low"));
        session.saveOrUpdate(new Group("F2835med"));
        session.saveOrUpdate(new Group("F2835hi"));
        session.saveOrUpdate(new Group("F2835max"));
        session.saveOrUpdate(new Group("M3645low"));
        session.saveOrUpdate(new Group("M3645med"));
        session.saveOrUpdate(new Group("M3645hi"));
        session.saveOrUpdate(new Group("M3645max"));
        session.saveOrUpdate(new Group("F3645low"));
        session.saveOrUpdate(new Group("F3645med"));
        session.saveOrUpdate(new Group("F3645hi"));
        session.saveOrUpdate(new Group("F45max"));
        session.saveOrUpdate(new Group("M45low"));
        session.saveOrUpdate(new Group("M45med"));
        session.saveOrUpdate(new Group("M45hi"));
        session.saveOrUpdate(new Group("M45max"));
        session.saveOrUpdate(new Group("F45low"));
        session.saveOrUpdate(new Group("F45med"));
        session.saveOrUpdate(new Group("F45hi"));
        session.saveOrUpdate(new Group("F45max"));
    }

}
